package com.xm.xmapp;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.app.Type.F_Main;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.customview.TipDialog;
import com.xm.xmapp.dialog.BigcustomerTipDialog;
import com.xm.xmapp.dialog.BigcustomerTipDialog.OnDaKeHuDialog;
import com.xm.xmapp.fragment.MainLeftFragment;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.myinterface.MyDialogButton;
import com.xm.xmapp.request.bean.Confirm_joinRequest;
import com.xm.xmapp.request.bean.Confirm_quitRequest;
import com.xm.xmapp.request.bean.Get_big_customer_infoRequest;
import com.xm.xmapp.response.bean.ResponseBigCustomerInfo;
import com.xm.xmapp.response.bean.ResponseBigCustomerInviteInfo;
import com.xm.xmapp.response.bean.ResponseUserBean;
import com.xm.xmapp.tools.Tools;

public class BigCustomerActivity extends BaseActivity implements OnClickListener {

	private TextView big_customer_name, big_customer_number, big_customer_address, tuichu;
	public ResponseBigCustomerInviteInfo bean;
	private BigcustomerTipDialog daKeHuDialog;

	/**
	 * 1极光推送，用户被公司退出大客户
	 */

	@Override
	public Object dataChang(int flag, Object data) {
		switch (flag) {
		case Type.A_BigCustomer.JPUSH_OUT:
			finish();
			break;
		default:
			break;
		}
		return super.dataChang(flag, data);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_bigcustomer);
	}

	@Override
	public void initView() {

		tuichu = (TextView) findViewById(R.id.tuichu);
		big_customer_name = (TextView) findViewById(R.id.big_customer_name);
		big_customer_number = (TextView) findViewById(R.id.big_customer_number);
		big_customer_address = (TextView) findViewById(R.id.big_customer_address);
	}

	@Override
	public void initData() {

		setMyTitle("企业");
		bean = (ResponseBigCustomerInviteInfo) getIntent().getSerializableExtra("bean");
		if (bean == null) {
			Tools.showMessage("未获取到你的信息");
		} else {
			if (BigCustomerActivity.this.bean.join_big_customer == -1) {
				daKeHuDialog = new BigcustomerTipDialog(BigCustomerActivity.this);
				daKeHuDialog.show();
				daKeHuDialog.setOnDaKeHuDialog(new OnDaKeHuDialog() {

					@Override
					public void refuse() {
						confirm_joinRequest(0);
						finish();
					}

					@Override
					public void accept() {
						confirm_joinRequest(1);
					}
				});
				daKeHuDialog.setTextContent(bean.big_customer_name);
			} else {
				get_big_customer_info(bean.big_customer_id);
			}

		}

	}

	// 加入公司确认：1=确认加入，0=拒绝加入
	public void confirm_joinRequest(final int type) {
		Confirm_joinRequest requestBean = new Confirm_joinRequest();
		requestBean.big_customer_id = bean.big_customer_id;
		requestBean.type = type;
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				if (type == 1) {
					ResponseUserBean userBean = MyAppcation.getMyAppcation().getUserBean();
					userBean.is_big_customer_user = 1;
					MyAppcation.getMyAppcation().saveUserBean(userBean);
					BigCustomerActivity.this.bean.join_big_customer = type;
					MyAppcation.getMyAppcation().notifyDataSetChanged(MainLeftFragment.class, F_Main.DEFAULT);
					get_big_customer_info(bean.big_customer_id);
				} else {
					MyAppcation.getMyAppcation().notifyDataSetChanged(MainLeftFragment.class, F_Main.DEFAULT);
				}
			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});

	}

	private void get_big_customer_info(int big_customer_id) {

		Get_big_customer_infoRequest requestBean = new Get_big_customer_infoRequest();
		requestBean.big_customer_id = big_customer_id;
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				ResponseBigCustomerInfo bean = JSON.parseObject(response, ResponseBigCustomerInfo.class);
				big_customer_name.setText(bean.big_customer_name);
				big_customer_number.setText(bean.big_customer_number);
				big_customer_address.setText(bean.big_customer_address);
			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});
	}

	@Override
	public void initEvent() {
		tuichu.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.tuichu:

			if (bean != null) {
				TipDialog tipDialog = new TipDialog(this, "温馨提示", "确定要退出当前企业吗?", new MyDialogButton() {

					@Override
					public void setTitle(int flag, String str) {
						confirm_quitRequest();
					}
				});
				tipDialog.show();
			} else {
				Tools.showMessage("为获取到您的信息");
			}

			break;

		default:
			break;
		}
	}

	private void confirm_quitRequest() {
		Confirm_quitRequest confirm_quitRequest = new Confirm_quitRequest();
		confirm_quitRequest.big_customer_id = bean.big_customer_id;
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, confirm_quitRequest, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				ResponseUserBean bean = MyAppcation.getMyAppcation().getUserBean();
				bean.is_big_customer_user = 0;
				MyAppcation.getMyAppcation().saveUserBean(bean);
				MyAppcation.getMyAppcation().notifyDataSetChanged(MainLeftFragment.class,F_Main.DEFAULT);
				MyAppcation.getMyAppcation().notifyDataSetChanged(MainActivity.class, Type.A_Main.QUIT_BIGCUSTOM_CODE);
				finish();
			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});
	}

}
